// 玩家角色类
class Player {
    constructor(characterType) {
        this.characterType = characterType;
        this.initializeStats();
        this.initializeStartingDeck();
    }

    // 初始化属性
    initializeStats() {
        // 初始化状态管理器
        this.statusManager = new StatusManager();
        
        switch (this.characterType) {
            case 'xu':
                this.name = '旭';
                this.maxHealth = 100;
                this.health = 100;
                this.maxEnergy = 3;
                this.energy = 3;
                this.maxHandSize = 10;
                this.block = 0;
                this.strength = 2;
                this.dexterity = 0;
                this.intelligence = 0;
                break;
            case 'ling':
                this.name = '玲';
                this.maxHealth = 80;
                this.health = 80;
                this.maxEnergy = 4;
                this.energy = 4;
                this.maxHandSize = 10;
                this.block = 0;
                this.strength = 0;
                this.dexterity = 3;
                this.intelligence = 0;
                break;
            case 'che':
                this.name = '澈';
                this.maxHealth = 70;
                this.health = 70;
                this.maxEnergy = 3;
                this.energy = 3;
                this.maxHandSize = 10;
                this.block = 0;
                this.strength = 0;
                this.dexterity = 0;
                this.intelligence = 2;
                break;
        }

        this.baseStrength = this.strength || 0;
        this.baseDexterity = this.dexterity || 0;
        this.baseIntelligence = this.intelligence || 0;
        this.battleAttributeGains = { strength: 0, dexterity: 0, intelligence: 0 };
        this.turnAttributeGains = { strength: 0, dexterity: 0, intelligence: 0 };
        this.recalculateAttributes();
    }

    // 初始化起始卡组
    initializeStartingDeck() {
        this.startingDeck = [];
        
        switch (this.characterType) {
            case 'xu':
                // 旭的起始卡组 - 力量型
                this.startingDeck = [
                    new Card('重拳', 1, 'attack', 8, '造成8点伤害', [], false, 'common'),
                    new Card('重拳', 1, 'attack', 8, '造成8点伤害', [], false, 'common'),
                    new Card('重拳', 1, 'attack', 8, '造成8点伤害', [], false, 'common'),
                    new Card('防御', 1, 'defend', 0, '获得5点护甲', [], false, 'common'),
                    new Card('防御', 1, 'defend', 0, '获得5点护甲', [], false, 'common'),
                    new Card('愤怒', 1, 'skill', 0, '获得2点力量', [], false, 'common'),
                    new Card('愤怒', 1, 'skill', 0, '获得2点力量', [], false, 'common'),
                    new Card('重击', 2, 'attack', 12, '造成12点伤害', [], false, 'common'),
                    new Card('重击', 2, 'attack', 12, '造成12点伤害', [], false, 'common'),
                    new Card('坚韧', 1, 'skill', 0, '获得3点护甲和1点力量', [], false, 'common')
                ];
                break;
            case 'ling':
                // 玲的起始卡组 - 敏捷型
                this.startingDeck = [
                    new Card('快速攻击', 0, 'attack', 4, '造成4点伤害', [], false, 'common'),
                    new Card('快速攻击', 0, 'attack', 4, '造成4点伤害', [], false, 'common'),
                    new Card('快速攻击', 0, 'attack', 4, '造成4点伤害', [], false, 'common'),
                    new Card('快速攻击', 0, 'attack', 4, '造成4点伤害', [], false, 'common'),
                    new Card('闪避', 1, 'defend', 0, '获得3点护甲，抽1张牌', [], false, 'common'),
                    new Card('闪避', 1, 'defend', 0, '获得3点护甲，抽1张牌', [], false, 'common'),
                    new Card('连击', 1, 'attack', 6, '造成6点伤害，抽1张牌', [], false, 'common'),
                    new Card('连击', 1, 'attack', 6, '造成6点伤害，抽1张牌', [], false, 'common'),
                    new Card('敏捷', 1, 'skill', 0, '获得2点敏捷', [], false, 'common'),
                    new Card('敏捷', 1, 'skill', 0, '获得2点敏捷', [], false, 'common')
                ];
                break;
            case 'che':
                // 澈的起始卡组 - 成长型（平衡调整：增加基础攻击和防御牌）
                this.startingDeck = [
                    // 基础攻击牌（5张，确保有足够的输出）
                    new Card('基础攻击', 1, 'attack', 5, '造成5点伤害', [], false, 'common'),
                    new Card('基础攻击', 1, 'attack', 5, '造成5点伤害', [], false, 'common'),
                    new Card('基础攻击', 1, 'attack', 5, '造成5点伤害', [], false, 'common'),
                    new Card('基础攻击', 1, 'attack', 5, '造成5点伤害', [], false, 'common'),
                    new Card('基础攻击', 1, 'attack', 5, '造成5点伤害', [], false, 'common'),
                    // 基础防御牌（3张，确保有足够的防御）
                    new Card('基础防御', 1, 'defend', 0, '获得4点护甲', [], false, 'common'),
                    new Card('基础防御', 1, 'defend', 0, '获得4点护甲', [], false, 'common'),
                    new Card('基础防御', 1, 'defend', 0, '获得4点护甲', [], false, 'common'),
                    // 成长技能牌（2张，体现成长型特点）
                    new Card('学习', 1, 'skill', 0, '获得1点智力，抽1张牌', [], false, 'common'),
                    new Card('成长', 2, 'skill', 0, '获得1点力量、敏捷和智力', [], false, 'common')
                ];
                break;
        }
    }

    // 添加护甲
    addBlock(amount) {
        this.block += amount;
    }

    // 受到伤害（考虑状态效果）
    takeDamage(damage) {
        // 计算易伤和脆弱（受到的伤害增加）
        const vulnerable = this.statusManager ? this.statusManager.getStatusValue('易伤') : 0;
        const fragile = this.statusManager ? this.statusManager.getStatusValue('脆弱') : 0;
        const damageMultiplier = 1 + (vulnerable * 0.25) + (fragile * 0.5); // 易伤+25%/层，脆弱+50%/层
        const adjustedDamage = Math.floor(damage * damageMultiplier);
        
        const actualDamage = Math.max(0, adjustedDamage - this.block);
        this.health -= actualDamage;
        this.block = Math.max(0, this.block - adjustedDamage);
        
        if (this.health < 0) {
            this.health = 0;
        }
    }

    // 治疗
    heal(amount) {
        this.health = Math.min(this.maxHealth, this.health + amount);
    }

    // 获得力量
    addStrength(amount, scope = 'turn') {
        this.applyAttributeGain('strength', amount, scope);
    }

    // 获得敏捷
    addDexterity(amount, scope = 'turn') {
        this.applyAttributeGain('dexterity', amount, scope);
    }

    // 获得智力
    addIntelligence(amount, scope = 'turn') {
        this.applyAttributeGain('intelligence', amount, scope);
    }

    applyAttributeGain(attribute, amount, scope = 'turn') {
        if (!amount) return;
        const normalized = attribute.toLowerCase();
        const cap = normalized.charAt(0).toUpperCase() + normalized.slice(1);
        const baseKey = `base${cap}`;
        this.battleAttributeGains = this.battleAttributeGains || { strength: 0, dexterity: 0, intelligence: 0 };
        this.turnAttributeGains = this.turnAttributeGains || { strength: 0, dexterity: 0, intelligence: 0 };

        if (scope === 'permanent') {
            this[baseKey] = (this[baseKey] || 0) + amount;
        } else if (scope === 'battle') {
            this.battleAttributeGains[normalized] = (this.battleAttributeGains[normalized] || 0) + amount;
        } else {
            this.turnAttributeGains[normalized] = (this.turnAttributeGains[normalized] || 0) + amount;
        }
        this.recalculateAttributes();
    }

    recalculateAttributes() {
        this.strength = (this.baseStrength || 0) + (this.battleAttributeGains?.strength || 0) + (this.turnAttributeGains?.strength || 0);
        this.dexterity = (this.baseDexterity || 0) + (this.battleAttributeGains?.dexterity || 0) + (this.turnAttributeGains?.dexterity || 0);
        this.intelligence = (this.baseIntelligence || 0) + (this.battleAttributeGains?.intelligence || 0) + (this.turnAttributeGains?.intelligence || 0);
    }

    resetTurnAttributeGains() {
        this.turnAttributeGains = { strength: 0, dexterity: 0, intelligence: 0 };
        this.recalculateAttributes();
    }

    resetBattleAttributeGains() {
        this.battleAttributeGains = { strength: 0, dexterity: 0, intelligence: 0 };
        this.resetTurnAttributeGains();
    }

    startNewBattle() {
        this.resetBattleAttributeGains();
    }

    startNewTurn() {
        this.resetTurnAttributeGains();
    }

    // 获取总伤害加成
    getDamageBonus() {
        const statusBonusStr = this.statusManager ? this.statusManager.getStatusValue('力量') : 0;
        const statusBonusInt = this.statusManager ? this.statusManager.getStatusValue('智力') : 0;
        if (this.characterType === 'che') {
            return this.strength + this.intelligence + statusBonusStr + statusBonusInt;
        }
        return this.strength + statusBonusStr;
    }

    // 获取总护甲加成
    getBlockBonus() {
        const statusBonusDex = this.statusManager ? this.statusManager.getStatusValue('敏捷') : 0;
        return this.dexterity + statusBonusDex;
    }

    // 获取技能效果加成
    getSkillBonus() {
        const statusBonusInt = this.statusManager ? this.statusManager.getStatusValue('智力') : 0;
        return this.intelligence + statusBonusInt;
    }
}

// 卡牌类
class Card {
    constructor(name, cost, type, value, description, effects = [], isSpell = false, rarity = 'common', role = null) {
        this.name = name;
        this.cost = cost;
        this.type = type; // attack, defend, skill
        this.value = value; // 基础数值
        this.description = description;
        this.effects = effects; // 特殊效果
        this.upgraded = false;
        this.isSpell = isSpell; // 是否为法术牌（使用智力加成）
        this.rarity = rarity; // 稀有度：'common'(普通), 'uncommon'(罕见), 'rare'(稀有)
        this.role = role || this.determineDefaultRole();
    }

    determineDefaultRole() {
        if (Card.powerCardNames.has(this.name)) {
            return 'power';
        }
        if (this.type === 'attack') return 'attack';
        if (this.type === 'defend') return 'defend';
        return 'skill';
    }

    // 升级卡牌
    upgrade() {
        if (this.upgraded) return;
        
        this.upgraded = true;
        this.cost = Math.max(0, this.cost - 1);
        
        switch (this.type) {
            case 'attack':
                this.value = Math.floor(this.value * 1.5);
                break;
            case 'defend':
                this.value = Math.floor(this.value * 1.5);
                break;
            case 'skill':
                // 技能卡升级效果根据具体卡牌而定
                break;
        }
        
        this.updateDescription();
    }

    // 更新描述
    updateDescription() {
        switch (this.type) {
            case 'attack':
                this.description = `造成${this.value}点伤害`;
                break;
            case 'defend':
                this.description = `获得${this.value}点护甲`;
                break;
            case 'skill':
                // 技能描述保持不变或根据具体效果更新
                break;
        }
    }

    // 获取实际伤害
    getDamage(player) {
        if (this.type !== 'attack') return 0;
        // 法术牌使用智力加成，普通攻击牌使用力量加成（或混合加成）
        if (this.isSpell) {
            // 法术牌：使用智力加成
            return this.value + player.getSkillBonus();
        } else {
            // 普通攻击牌：使用力量加成（澈角色已包含智力混合加成）
            return this.value + player.getDamageBonus();
        }
    }

    // 获取实际护甲
    getBlock(player) {
        if (this.type !== 'defend') return 0;
        return this.value + player.getBlockBonus();
    }
}

Card.powerCardNames = new Set([
    '愤怒',
    '强化',
    '敏捷',
    '智慧',
    '成长',
    '创新',
    '力量爆发',
    '敏捷提升',
    '智慧之光',
    '全面成长',
    '适应',
    '突破',
    '觉醒',
    '节奏掌控',
    '心无旁骛',
    '专注',
    '奥术智慧'
]);
